package com.neu.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.neu.R;

public class LinearActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_linear);
        //请求权限
        checkReadPermission(Manifest.permission.READ_SMS,REQUEST_PERMISSION);
    }

    public static final int REQUEST_PERMISSION = 10111; //请求码

    /**
     * 判断是否有某项权限
     * @param string_permission 权限
     * @param request_code 请求码
     * @return
     */
    public boolean checkReadPermission(String string_permission,int request_code) {
        boolean flag = false;
        if (ContextCompat.checkSelfPermission(this, string_permission) == PackageManager.PERMISSION_GRANTED) {//已有权限
            flag = true;
        } else {//申请权限
            ActivityCompat.requestPermissions(this, new String[]{string_permission}, request_code);
        }
        return flag;
    }

    public void btn1Click(View view) {


        ContentResolver crs = getContentResolver();
        Uri uri = Uri.parse("content://sms");
        Cursor cusor = crs.query(uri, new String[]{"address", "date", "body"}, null, null, null);
        if(cusor!=null&& cusor.getCount()>0){
            while (cusor.moveToNext()){
                String address = cusor.getString(cusor.getColumnIndex("address"));
                long timesec = cusor.getLong(cusor.getColumnIndex("date"));
                String body = cusor.getString(cusor.getColumnIndex("body"));
                Log.d("TAG", address+":"+timesec+":"+body);
            } }

    }

    public void btnProVider(View view) {
        //使用 ContentProvider 进行私有数据库的访问
        ContentResolver cr = getContentResolver();
        Uri uri = Uri.parse("content://com.neu.provider.my/insert");
        ContentValues values = new ContentValues();
        values.put("name", "zhangsan");
        values.put("snum", 10001);
        Uri uriid = cr.insert(uri, values);
        long lid = ContentUris.parseId(uriid);
        Log.d("TAG", lid+"学号的学生被添加了");
    }
}